﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;

namespace FYPDemo
{
    public partial class Reward_Maintenance : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindDropdownlist();
                bindActivatedPromotion();
            }

        }

        protected void bindDropdownlist()
        {
            DataTable dt = new DataTable();
            dt = BackOffice_Maintenance.getRewardPromotion();

            ddlPromotion.DataSource = dt;
            ddlPromotion.DataTextField = "Promotion";
            ddlPromotion.DataValueField = "ID";
            ddlPromotion.DataBind();            
        }

        protected void bindActivatedPromotion()
        {
            DataTable dt = new DataTable();
            dt = BackOffice_Maintenance.getActivedPromotion();

            if (dt.Rows.Count > 0)
            {

                ddlPromotion.SelectedValue = dt.Rows[0][0].ToString();
                txtPromotion.Text = dt.Rows[0][3].ToString();
                txtRate.Text = dt.Rows[0][1].ToString();
                txtAddOn.Text = dt.Rows[0][2].ToString();
                rbActive.SelectedValue = dt.Rows[0][4].ToString();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            int promotion = Convert.ToInt32(ddlPromotion.SelectedValue);

            DataTable dt = new DataTable();

            dt = BackOffice_Maintenance.getSelectedPromotion(promotion);

            if (dt.Rows.Count > 0)
            {
                txtPromotion.Text = dt.Rows[0][3].ToString();
                txtRate.Text = dt.Rows[0][1].ToString();
                txtAddOn.Text = dt.Rows[0][2].ToString();
                rbActive.SelectedValue = dt.Rows[0][4].ToString();
            }
        }

        protected void btnChange_Click(object sender, EventArgs e)
        {
            int promoID = Convert.ToInt32(ddlPromotion.SelectedValue);
            double rate = Convert.ToDouble(txtRate.Text);
            double addOn = Convert.ToDouble(txtAddOn.Text);
            string Promotion = txtPromotion.Text;
            int stat = Convert.ToInt32(rbActive.SelectedValue);

            BackOffice_Maintenance.UpdateRewardPromotion(promoID, rate, addOn, Promotion, stat);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            //int promoID = Convert.ToInt32(ddlPromotion.SelectedValue);
            double rate = Convert.ToDouble(txtRate.Text);
            double addOn = Convert.ToDouble(txtAddOn.Text);
            string Promotion = txtPromotion.Text;
            int stat = Convert.ToInt32(rbActive.SelectedValue);

            BackOffice_Maintenance.AddRewardPromotion(rate, addOn, Promotion, stat);
        }

        protected void btnAdd2_Click(object sender, EventArgs e)
        {
            int result = 0;
            string prodName = txtProdudctName.Text;

            int checkName = BackOffice_Maintenance.checkRewardNameRedundant(prodName);

            if (checkName <= 0)
            {
                int point = Convert.ToInt32(txtPoint.Text);
                int quantity = Convert.ToInt32(txtQuantity.Text);
                int status = Convert.ToInt32(rbStatus.SelectedValue);

                if (FileUpload1.HasFile)
                {
                    HttpPostedFile theFile = FileUpload1.PostedFile;
                    string theDir = "RedeemProduct";
                    uploadFile(theFile, theDir, prodName);
                    ImgUpload.ImageUrl = "/RedeemProduct/" + prodName + ".jpg";
                }

                string imagePath = ImgUpload.ImageUrl.ToString();

                result = BackOffice_Maintenance.addRewardProduct(prodName, point, quantity, status, imagePath);

                if (result > 0)
                {
                    MessageBox.Show("Successfull add the reward item");
                }
            }

            else
            {
                MessageBox.Show("The name already being used.");
            }
        }

        private void uploadFile(HttpPostedFile theFile, string uploadTo, string filename)
        {
            if (theFile != null && theFile.ContentLength > 0)
            {
                if (!Directory.Exists(Server.MapPath("~/" + uploadTo)))
                    Directory.CreateDirectory(Server.MapPath("~/" + uploadTo));

                string fname = filename + Path.GetExtension(Path.GetFileName(theFile.FileName));
                theFile.SaveAs(Server.MapPath(Path.Combine("~/" + uploadTo + "/", fname)));

            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string username = "TempReward";
            string uploadTo = "Reward";

            if (FileUpload1.HasFile)
            {
                if (Directory.Exists(Server.MapPath("~/" + uploadTo + "/" + username)))
                {
                    System.IO.File.Delete(Server.MapPath("~/" + uploadTo + "/" + username));
                }

                else
                {
                    HttpPostedFile theFile = FileUpload1.PostedFile;
                    Session["PostedFile"] = theFile;
                    uploadFile(theFile, uploadTo, username);
                    ImgUpload.ImageUrl = "~/" + uploadTo + "/" + username + ".jpg";
                }
            }
        }

        protected void btnRewardList_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("Reward_Maintenance2.aspx");
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            Response.Redirect("Reward_Maintenance2.aspx");
        }        
    }
}